<template>
  <el-dialog
    :title="$t('flyparam.reference_table_preview')"
    v-bind="$attrs"
    width="80%"
    v-on="$listeners"
    @open="open"
    @close="close"
  >
    <el-row v-loading="loading">
      <el-col>
        <div id="content" class="layout-preview-container">
          <el-scrollbar style="height: 100%;" wrap-class="scrollbar-wrapper">
            <el-form @submit.native.prevent>
              <grid-layout
                ref="gridlayout"
                :layout.sync="layout"
                :col-num="3"
                :row-height="36"
                :min-h="2"
                :is-draggable="false"
                :is-resizable="false"
                :is-mirrored="false"
                :auto-size="false"
                :vertical-compact="true"
                :margin="[10, 10]"
                :use-css-transforms="true"
              >
                <grid-item
                  v-for="item in layout"
                  :key="item.i"
                  style="touch-action: none"
                  :x="item.x"
                  :y="item.y"
                  :w="item.w"
                  :h="item.h"
                  :i="item.i"
                >
                  <parameter-card disabled :closable="false" :item="item" :form="form" />
                <!-- @select-reference="(id) => handleSelectReference(id)" -->
                </grid-item>
              </grid-layout>
            </el-form>
          </el-scrollbar>
        </div>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getTemplateTableLayoutList } from '@/api/parameter'
import ParameterCard from './parameter'
import VueGridLayout from 'vue-grid-layout'
import { types } from '../constant'

export default {
  components: {
    GridLayout: VueGridLayout.GridLayout,
    GridItem: VueGridLayout.GridItem,
    ParameterCard
  },
  props: {
    tableId: {
      type: String
    },
    templateId: String
  },
  data() {
    return {
      layout: [],
      form: {},
      loading: false,
      types: Object.freeze(types)
    }
  },
  methods: {
    fetchData() {
      this.loading = true
      getTemplateTableLayoutList(this.templateId, this.tableId)
        .then((res) => {
          this.layout = res.map((item) => ({
            ...item,
            i: item.paramLayoutId,
            title: item.title,
            valueType: item.valueType,
            type: item.type,
            w: item.w,
            y: item.y,
            x: item.x,
            h: item.h,
            label: item.label,
            edit: false,
            defaultValues:
              item.valueType === this.types.REFERENCE
                ? item.defaultValues.map((item) => item.name)
                : item.defaultValues && item.defaultValues.length === 1
                  ? item.defaultValues[0]
                  : ''
          }))
          this.layout.forEach((data) => {
            this.form[data.i] = data.defaultValues || ''
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
    open() {
      this.fetchData()
    },
    close() {
      this.layout = []
    }
  }
}
</script>

<style scoped lang="scss">
.layout-preview-container {
  height: 500px;

  &::v-deep {
    .el-scrollbar__wrap {

    }
    .el-scrollbar__view, .el-form {
      height: 100%;
    }

  }
}
</style>
